import { createRouter, createWebHashHistory } from "vue-router";
import routes from "./routes";
import LsObj from "@/common/core/ls";

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from) => {
  to?.meta?.title && (document.title = to.meta.title);
  if (to.name !== "login") {
    const token = LsObj.common.token.get();
    if (!token) {
      return `/login?from=${from.fullPath}`;
    }
    return true;
  }
  return true;
});

export default router;
